/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.frontend.animal;

import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.tos.AnimalTo;
import pa165.dominating.species.tos.EnvironmentTo;
import pa165.dominatingspecies.service.impl.AnimalServiceImpl;

/**
 *
 * @author Filip Dinh
 */
@PreAuthorize("hasRole('ADMIN')")
public class DeleteAnimalLink extends Link<Void> {

    @SpringBean(name = "animalService")
    private AnimalServiceImpl animalService;
    private final AnimalTo animal;
    private EnvironmentTo filter;

    public DeleteAnimalLink(String componentId, AnimalTo animal, EnvironmentTo filter) {
        super(componentId);
        this.animal = animal;
        this.filter = filter;
    }

    @Override
    public void onClick() {
        boolean thrown = false;
        try {
            animalService.deleteAnimal(animal);
        } catch (DataAccessException ex) {
            thrown = true;
            getSession().warn(new StringResourceModel("animalRemoveFailed", this, null, new Object[]{animal.getId()}).getString());
            System.out.println(ex.getMessage());
            setResponsePage(new AnimalsPage(filter));
        }
        if (!thrown) {
            getSession().info(new StringResourceModel("animalRemoveSucc", this, null, new Object[]{animal.getId()}).getString());
            setResponsePage(new AnimalsPage(filter));
        }
    }
}
